<template>
  <div
    class="page_body"
    v-loading.fullscreen.lock="loadingKey"
    :element-loading-text="loadingText"
  >
    <!-- 面包屑 -->
    <el-row>
      <el-col :span="24" class="page_box_top">
        <i class="el-icon-s-fold"></i>
        <el-breadcrumb separator="/" class="page_box_top_nav">
          <el-breadcrumb-item class="breadcrumb_active_item"
            >部门管理</el-breadcrumb-item
          >
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <div class="department_manager_content">
        <!-- 按钮栏 -->
        <el-row>
          <el-col :span="24">
            <div class="department_manager_top">
              <el-button
                size="mini"
                type="primary"
                @click="addFn()"
                v-if="userType == 0"
                >新增</el-button
              >
              <el-input
                v-model="searchKey"
                placeholder="请输入关键词"
                size="mini"
                style="width: 200px; margin-left: 10px"
              ></el-input>
              <el-button
                type="primary"
                size="mini"
                style="margin-left: 5px"
                @click="searchFn()"
                >搜索</el-button
              >
            </div>
          </el-col>
        </el-row>
        <!-- 数据列表 -->
        <div
          v-if="
            departmentListData != undefined && departmentListData.length > 0
          "
        >
          <el-row>
            <el-col :span="24">
              <el-table
                :data="departmentListData"
                border
                size="mini"
                style="width: 100%"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                  align="center"
                >
                </el-table-column>

                <el-table-column
                  prop="department_name"
                  label="部门"
                  align="center"
                  min-width="140"
                >
                </el-table-column>

                <el-table-column
                  prop="view_type"
                  label="查看名单"
                  width="140"
                  align="center"
                  :formatter="viewseStatusFn"
                ></el-table-column>
                <el-table-column
                  prop="collect_type"
                  label="报名字段"
                  width="140"
                  align="center"
                  :formatter="formatterReleaseStatusFn"
                ></el-table-column>

                <el-table-column
                  prop="created_at"
                  label="创建时间"
                  min-width="180"
                  align="center"
                ></el-table-column>

                <el-table-column
                  prop="updated_at"
                  label="更新时间"
                  min-width="180"
                  align="center"
                ></el-table-column>

                <el-table-column
                  v-if="userType == 0"
                  label="操作"
                  min-width="200"
                  align="center"
                >
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="success"
                      @click="editFn(scope.$index, scope.row)"
                      :disabled="scope.row.release_status == 1"
                      >编辑</el-button
                    >

                    <el-button
                      size="mini"
                      type="danger"
                      @click="deleteFn(scope.$index, scope.row)"
                      >删除</el-button
                    >
                    <el-button
                      size="mini"
                      type="primary"
                      @click="setup(scope.$index, scope.row)"
                      >设置</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="department_manager_pagination">
                <el-pagination
                  @size-change="listSizeChange"
                  @current-change="listCurrentChange"
                  :current-page="currentPage"
                  :page-sizes="pageSizes"
                  :page-size="pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total"
                >
                </el-pagination>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-row v-else>
          <el-empty description="暂无数据"></el-empty>
        </el-row>
        <!-- 弹窗 -->
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          :close-on-click-modal="false"
          width="500px"
        >
          <el-form
            ref="dialogDetailForm"
            :model="detailFormData"
            :rules="detailFormRules"
            v-if="dialogBtnKey == 1 || dialogBtnKey == 2"
          >
            <el-form-item
              label="部门"
              prop="department_name"
              :label-width="formLabelWidth"
            >
              <el-input
                size="small"
                v-model="detailFormData.department_name"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="部门标识"
              prop="mark"
              :label-width="formLabelWidth"
            >
              <el-input size="small" v-model="detailFormData.mark"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="dialogclose()">取 消</el-button>
            <el-button
              size="small"
              type="primary"
              v-if="userType == 0"
              @click="dialogDetailFormSubmit()"
              >确 定</el-button
            >
          </span>
        </el-dialog>
        <el-dialog title="部门设置" :visible.sync="setrp" width="30%">
          <div class="but">
            <el-radio-group v-model="radio" @change="agreeChange">
              <el-form>
                <el-form-item label="报名字段：" :label-width="formLabelWidth">
                  <el-radio label="1" border>全部</el-radio>
                  <el-radio label="2" border>部分</el-radio>
                </el-form-item>
              </el-form>
            </el-radio-group>
          </div>
          <div id="buts">
            <el-radio-group v-model="quanradio" @change="quanagreeChange">
              <el-form>
                <el-form-item label="查看名单：" label-width="90px">
                  <el-radio label="1" border>全部名单</el-radio>
                  <el-radio label="2" border>所属部门</el-radio>
                </el-form-item>
              </el-form>
            </el-radio-group>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="quxiao">取 消</el-button>
            <el-button type="primary" @click="setok">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-row>
  </div>
</template>
<style src="../../assets/css/department/departmentList.css" scoped></style>

<script>
export default {
  name: "DepartmentList",
  components: {},
  data() {
    return {
      userType: this.$store.state.loginUserType == "ADMINLOGINUSERCH" ? 0 : 1, // 用户类型;1. 普通; 0. 超级;
      baseAPIUrl: this.$store.state.baseAPIUrl,
      departmentListData: [], // 数据列表
      currentPage: 1, // 当前页
      pageSizes: [10, 20, 50, 100], // 可选页容量
      pageSize: 10, // 当前页容量
      total: 0, // 总条数
      searchKey: "", // 搜索关键词
      loadingKey: false,
      loadingText: "",
      dialogTitle: "", // 弹窗标题
      dialogBtnKey: "", // 弹窗确认按钮控制; 1. 新增部门确认;2. 修改部门确认;
      dialogVisible: false, // 弹窗显示
      setrp: false, //弹窗显示
      radio: "1",
      quanradio: "1",
      collecttype: "",
      ids: "", //部门id
      but: "",
      detailFormData: {
        id: "",
        department_name: "",
        mark: "",
        view_type: "",
      }, // 部门弹窗数据
      detailFormRules: {
        department_name: [
          { required: true, message: "请输入部门", trigger: "blur" },
          {
            min: 2,
            max: 12,
            message: "长度在 2 到 12 个字符",
            trigger: "blur",
          },
        ],
        mark: [
          { required: true, message: "请输入部门标识", trigger: "blur" },
          { max: 12, message: "长度不超过 12 个字符", trigger: "blur" },
        ],
      },
      formLabelWidth: "90px",
      releaseStatusList: [
        {
          key: 2,
          value: "部分",
        },
        {
          key: 1,
          value: "全部",
        },
      ], // 发布状态
      releasesList: [
        {
          key: 2,
          value: "所属部门",
        },
        {
          key: 1,
          value: "全部名单",
        },
      ], // 发布状态
    };
  },
  created: function () {
    this.getListDataFn();
  },
  methods: {
    // 获取列表数据
    getListDataFn: function () {
      let that = this;
      let url = "/admin/department/index";
      let data = {
        page: this.currentPage,
        perPage: this.pageSize,
        keywords: this.searchKey,
      };
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            that.departmentListData = response.list;
            that.total = response.total_count;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 收集状态格式化
    formatterReleaseStatusFn: function (row) {
      let status = row.collect_type;
      let text = "";
      this.releaseStatusList.forEach(function (item) {
        if (item.key == status) {
          text = item.value;
        }
      });
      return text;
    },
    // 查看名单
    viewseStatusFn(row) {
      let status = row.view_type;
      let text = "";
      this.releasesList.forEach(function (item) {
        if (item.key == status) {
          text = item.value;
        }
      });
      return text;
    },

    // 新增部门
    addFn: function () {
      let that = this;
      this.dialogTitle = "新增部门";
      this.dialogBtnKey = 1;
      this.detailFormData = {
        id: "",
        department_name: "",
        mark: "",
      };
      this.$nextTick(function () {
        that.dialogVisible = true;
        that.$refs.dialogDetailForm.clearValidate();
      });
    },
    // 修改部门
    editFn: function (index, row) {
      this.dialogTitle = "编辑部门";
      this.dialogBtnKey = 2;
      this.detailFormData = {
        id: row.id,
        department_name: row.department_name,
        mark: row.mark,
      };
      this.$nextTick(function () {
        this.dialogVisible = true;
      });
    },
    // 部门保存按钮
    dialogDetailFormSubmit: function () {
      let that = this;
      let url = "";
      let data = {};
      if (this.dialogBtnKey == 2) {
        url = "/admin/department/update";
        data = {
          id: this.detailFormData.id,
          department_name: this.detailFormData.department_name,
          mark: this.detailFormData.mark,
        };
      } else {
        url = "/admin/department/store";
        data = {
          department_name: this.detailFormData.department_name,
          mark: this.detailFormData.mark,
        };
      }
      this.$refs.dialogDetailForm.validate((valid) => {
        if (!valid) {
          return false;
        } else {
          that
            .$confirm("确认提交?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
            })
            .then(() => {
              that.loadingText = "处理中...";
              that.loadingKey = true;
              that.axios
                .post(url, data)
                .then((response) => {
                  that.loadingKey = false;
                  if (response.code == 0) {
                    if (that.dialogBtnKey == 2) {
                      that.$message.success("修改成功");
                    } else {
                      that.$message.success("新增成功");
                    }
                    that.dialogclose();
                    that.getListDataFn();
                  } else {
                    that.$message.error(response.msg);
                  }
                })
                .catch(function (error) {
                  that.loadingKey = false;
                  console.log(error);
                });
            })
            .catch(() => {});
        }
      });
    },

    // 关闭弹窗
    dialogclose: function () {
      this.dialogVisible = false;
    },

    // 删除
    deleteFn: function (index, row) {
      let that = this;
      this.$confirm("确认删除该部门?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          let url = "/admin/department/destroy";
          let data = {
            id: row.id,
          };
          that.loadingText = "处理中...";
          that.loadingKey = true;
          that.axios
            .post(url, data)
            .then((response) => {
              that.loadingKey = false;
              if (response.code == 0) {
                that.$message.success("删除成功");
                
                let next = that.currentPage - 1;
                if (that.departmentListData.length == 1) {
                  that.currentPage = next >= 1 ? next : 1;
                }
                that.getListDataFn();
              } else {
                that.$message.error(response.msg);
              }
            })
            .catch(function (error) {
              that.loadingKey = false;
              console.log(error);
            });
        })
        .catch(() => {});
    },
    //设置按钮
    setup(index, row) {
      this.ids = row.id;
      this.radio = row.collect_type.toString();
      this.quanradio = row.view_type.toString();
      // this.radio.toString();
      // this.collecttype = this.but;
      console.log(row);
      this.setrp = true;
    },
    //弹窗取消
    quxiao() {
      this.setrp = false;
    },
    agreeChange(val) {
      console.log(this.radio, this.ids);

      let url = "/admin/department/setupType";
      let data = {
        id: this.ids,
        collect_type: this.radio,
      };
      this.axios.post(url, data).then((res) => {
        if (res.code == 0) {
          console.log(res);
          // this.setrp = false;

          // this.radio = "1";
          console.log(this.radio, "caonim");
        }
      });
      // console.log(this.radio);
    },
    quanagreeChange() {
      let url = "/admin/department/changeViewType";
      let data = {
        id: this.ids,
        view_type: this.quanradio,
      };
      this.axios.post(url, data).then((res) => {
        if (res.code == 0) {
          console.log(res);
          // this.setrp = false;

          // this.quanradio = "1";
        }
      });
    },
    //弹窗确定 并且调起是否获取
    setok() {
      this.setrp = false;
      this.getListDataFn();
    },
    // 页容量改变
    listSizeChange: function (val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getListDataFn();
    },
    // 当前页改变
    listCurrentChange: function (val) {
      this.currentPage = val;
      this.getListDataFn();
    },
    // 搜索按钮
    searchFn: function () {
      this.currentPage = 1;
      this.getListDataFn();
    },
  },
};
</script>
<style scoped>
.but {
  display: flex;
}
#buts {
  display: flex;
}
#buts >>> .el-form-item__content {
  display: flex;
  margin-left: 0 !important;
}
#buts >>> .el-form-item__label {
  width: 0;
}
</style>

